﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AnesSystem.Controls;
using AnesSystem.ViewModels;
using AnesSystem.Models;
using System.Windows.Data;
using AnesSystem.Services;
using System.Collections.ObjectModel;

namespace AnesSystem.ViewModels
{
    public class AnesRecordCtrlViewModel : MultitonBaseViewModel
    {

        #region Variables
        protected Dictionary<int, AnesRecordItemCtrlViewModel> _anesRecordCtrlViewModels = new Dictionary<int, AnesRecordItemCtrlViewModel>();
        protected ObservableCollection<AnesRecordItemCtrlViewModel> _anesRecordItems = new ObservableCollection<AnesRecordItemCtrlViewModel>();
        protected int _totalPages;
        protected PatientLinkModel _patientLink;
        #endregion


        #region Constructor
        public AnesRecordCtrlViewModel()
        {

        }
        #endregion


        #region Properties

        public virtual Dictionary<int, AnesRecordItemCtrlViewModel> AnesRecordCtrlViewModels
        {
            get
            {
                return _anesRecordCtrlViewModels;
            }

            set
            {
                _anesRecordCtrlViewModels = value;
            }
        }

        public virtual ObservableCollection<AnesRecordItemCtrlViewModel> AnesRecordItems
        {
            get
            {
                return _anesRecordItems;
            }
            set
            {
                _anesRecordItems = value;
            }
        }


        #endregion


        #region 依赖属性


        #endregion


        #region Event


        #endregion


        #region PrivateMethods


        #endregion


        #region Public/ProtectedMethods

        public void GetAllOperationData()
        {
            InitDrugsCache();
            InitEventDataCache();
            InitVitalSignsDataCache();

        }


        /// <summary>
        /// 加载当前病人的手术信息
        /// </summary>
        public void LoadData()
        {
            GetAllOperationData();
            Refresh();
            for (int i = 1; i <= _totalPages; i++)
            {
                AnesRecordCtrlViewModels[i].Refresh();
            }
            RaisePropertyChanged("RecordCtrlList");

        }

        protected void InitVitalSignsDataCache()
        {
            _patientLink = DataManager.Instance.CurrentPatientLink;
            VitalSignDataCache vitalSignDataCache = OperationDataPool.Instance.CurOperationData.VitalSignDataCacheDic[Constants.ISANES];
            vitalSignDataCache.ClearCache();

            foreach (var item in _patientLink.VitalSignShow.VitalSign)
            {
                vitalSignDataCache.AddConfigParam(item);
            }
            vitalSignDataCache.RefreshRespEvent();

        }
        protected void InitEventDataCache()
        {
            _patientLink = DataManager.Instance.CurrentPatientLink;
            EventDataCache eventDataCache = OperationDataPool.Instance.CurOperationData.EventDataCacheDic[Constants.ISANES];
            eventDataCache.ClearCache();
            eventDataCache.DbSearch();

        }

        protected void InitDrugsCache()
        {
            _patientLink = DataManager.Instance.CurrentPatientLink;
            DrugsDataCache drugsDataCache = OperationDataPool.Instance.CurOperationData.DrugsDataCache;
            drugsDataCache.ClearCache();
            drugsDataCache.DbSearch();
        }

        public override void InitData()
        {
            if (!DataManager.Instance.PatientLinkCache.ContainsKey(DataManager.Instance.CurrentPatientLink.Id))
            {
                AnesRecordItems.Clear();
                _anesRecordCtrlViewModels.Clear();
                _totalPages = DataManager.Instance.CurrentPatientLink.Status.TotalPage;
                for (int page = 1; page <= _totalPages; page++)
                {
                    AnesRecordItemCtrlViewModel viewmode = new AnesRecordItemCtrlViewModel(page);
                    AnesRecordItems.Add(viewmode);
                    AnesRecordCtrlViewModels.Add(page, viewmode);
                }
                LoadData();
            }
            else
            {
                BaseTableControl ctrl = DataManager.Instance.AnesRecordCtrl;
                if (ctrl.IsNewCreate)
                {
                    ctrl.IsNewCreate = false;
                    AnesRecordItems.Clear();
                    _anesRecordCtrlViewModels.Clear();
                    _totalPages = DataManager.Instance.CurrentPatientLink.Status.TotalPage;
                    for (int page = 1; page <= _totalPages; page++)
                    {
                        AnesRecordItemCtrlViewModel viewmode = new AnesRecordItemCtrlViewModel(page);
                        AnesRecordItems.Add(viewmode);
                        AnesRecordCtrlViewModels.Add(page, viewmode);
                    }
                    LoadData();
                }
            }
        }

        public override void Refresh()
        {
            RealTimeSignsConfigCtrlViewModel.Instance.Refresh();

            RealTimeSignMainShowCtrlViewModel.Instance.Refresh();
        }

        #endregion


        #region EventHandlers


        #endregion
    }
}
